package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.common.vo.hosp.HospitalSetQueryVo;
import com.atguigu.yygh.common.vo.order.SignInfoVo;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.common.model.hosp.HospitalSet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取医院签名信息")
    @GetMapping("inner/getSignInfoVo/{hoscode}")
    public SignInfoVo getSignInfoVo(
            @ApiParam(name = "hoscode", value = "医院code", required = true)
            @PathVariable("hoscode") String hoscode) {
        return hospitalSetService.getSignInfoVo(hoscode);
    }

    // http://localhost:8201/admin/hosp/hospitalSet/findAll
    //查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        List<HospitalSet> hospitalSets = hospitalSetService.list();
        return Result.ok(hospitalSets);
    }

    //逻辑删除医院设置表信息
    @ApiOperation(value = "逻辑删除医院设置信息")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();


        }
    }

    //根据医院名称或者医院编号查询医院设置并分页
        @ApiOperation(value = "根据医院名称或者医院编号查询医院设置并分页")
        @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalQueryVo){

        Page<HospitalSet> page = new Page<>(current,limit);
        //根据条件查询
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String hosname = hospitalQueryVo.getHosname(); //医院名称
        String hoscode = hospitalQueryVo.getHoscode(); //医院编号
        if (!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hosname",hosname);
        }
        if (!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq("hoscode",hoscode);
        }
        //分页
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);
        return Result.ok(hospitalSetPage);
    }

    //4.添加医院设置
    @PostMapping("saveHospitalSet")
    @ApiOperation(value = "添加医院设置")
    public Result saveHospitalSet(@RequestBody(required = true) HospitalSet hospitalSet){
        //设置状态 1可用 0 不可用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        //添加医院
        boolean flag = hospitalSetService.save(hospitalSet);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //5.根据id获取医院设置
    @GetMapping("{id}")
    @ApiOperation(value = "根据id获取医院设置")
    public Result findById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }
    //6.修改医院设置
    @PutMapping("updateHospitalSet")
    @ApiOperation(value = "修改医院设置")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //7.批量删除医院设置
    @DeleteMapping("batchRemove")
    @ApiOperation(value = "批量删除医院设置")
    public Result batchId(@RequestBody List<Long> ids){
        boolean flag = hospitalSetService.removeByIds(ids);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //8.医院设置锁定和解锁
    @PutMapping("lock/{id}/{status}")
    @ApiOperation(value = "医院设置锁定和解锁")
    public Result u(@PathVariable Long id,
                    @PathVariable Integer status){
        //1.根据id获取医院
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //2.设置状态
        hospitalSet.setStatus(status);
        //3.最终修改
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return Result.ok(flag);
    }

    //9.发送签名密钥
    @PostMapping("sendHospitalSet")
    @ApiOperation(value = "发送签名密钥")
    public Result sendHospitalSet(@RequestBody HospitalSet hospitalSet){
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        String hosname = hospitalSet.getHosname();

        //Todo 发送短信
        return Result.ok();
    }

}
